package com.ruoyi.web.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.base.MesKeys;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IMesKeysService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 关重件数据Controller
 *
 * @author ruoyi
 * @date 2023-02-15
 */
@Api(tags = "关重件数据api")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/base/keys" )
public class MesKeysController extends BaseController {

    private final IMesKeysService iMesKeysService;

    /**
     * 查询关重件数据列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MesKeys mesKeys)
    {
        startPage();
        LambdaQueryWrapper<MesKeys> lqw = new LambdaQueryWrapper<MesKeys>();
        if (StringUtils.isNotBlank(mesKeys.getKeyName())){
            lqw.like(MesKeys::getKeyName ,mesKeys.getKeyName());
        }
        if (StringUtils.isNotBlank(mesKeys.getShortCode())){
            lqw.eq(MesKeys::getShortCode ,mesKeys.getShortCode());
        }
        if (StringUtils.isNotBlank(mesKeys.getScanCode())){
            lqw.eq(MesKeys::getScanCode ,mesKeys.getScanCode());
        }
        if (mesKeys.getStartTime() != null){
            lqw.eq(MesKeys::getStartTime ,mesKeys.getStartTime());
        }
        if (mesKeys.getEndTime() != null){
            lqw.eq(MesKeys::getEndTime ,mesKeys.getEndTime());
        }
        if (StringUtils.isNotBlank(mesKeys.getCompanyName())){
            lqw.like(MesKeys::getCompanyName ,mesKeys.getCompanyName());
        }
        if (StringUtils.isNotBlank(mesKeys.getItemCode())){
            lqw.eq(MesKeys::getItemCode ,mesKeys.getItemCode());
        }
        List<MesKeys> list = iMesKeysService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 查询关重件数据列表所有
     */
    @ApiOperation(value = "查询关重件数据列表所有")
    @GetMapping("/getList")
    public List<MesKeys> getList(MesKeys mesKeys)
    {
        LambdaQueryWrapper<MesKeys> lqw = new LambdaQueryWrapper<MesKeys>();
        if (StringUtils.isNotBlank(mesKeys.getKeyName())){
            lqw.like(MesKeys::getKeyName ,mesKeys.getKeyName());
        }
        if (StringUtils.isNotBlank(mesKeys.getShortCode())){
            lqw.eq(MesKeys::getShortCode ,mesKeys.getShortCode());
        }
        if (StringUtils.isNotBlank(mesKeys.getScanCode())){
            lqw.eq(MesKeys::getScanCode ,mesKeys.getScanCode());
        }
        if (mesKeys.getStartTime() != null){
            lqw.eq(MesKeys::getStartTime ,mesKeys.getStartTime());
        }
        if (mesKeys.getEndTime() != null){
            lqw.eq(MesKeys::getEndTime ,mesKeys.getEndTime());
        }
        if (StringUtils.isNotBlank(mesKeys.getCompanyName())){
            lqw.like(MesKeys::getCompanyName ,mesKeys.getCompanyName());
        }
        if (StringUtils.isNotBlank(mesKeys.getItemCode())){
            lqw.eq(MesKeys::getItemCode ,mesKeys.getItemCode());
        }
        List<MesKeys> list = iMesKeysService.list(lqw);
        return list;
    }

    /**
     * 导出关重件数据列表
     */
    @Log(title = "关重件数据" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(MesKeys mesKeys) {
        LambdaQueryWrapper<MesKeys> lqw = new LambdaQueryWrapper<MesKeys>(mesKeys);
        List<MesKeys> list = iMesKeysService.list(lqw);
        ExcelUtil<MesKeys> util = new ExcelUtil<MesKeys>(MesKeys. class);
        return util.exportExcel(list, "keys" );
    }

    /**
     * 获取关重件数据详细信息
     */
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iMesKeysService.getById(id));
    }

    /**
     * 新增关重件数据
     */
    @Log(title = "关重件数据" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesKeys mesKeys) {
        mesKeys.setCreateBy(SecurityUtils.getUsername());
        mesKeys.setCreateTime(new Date());
        return toAjax(iMesKeysService.save(mesKeys) ? 1 : 0);
    }

    /**
     * 修改关重件数据
     */
    @Log(title = "关重件数据" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesKeys mesKeys) {
        mesKeys.setUpdateBy(SecurityUtils.getUsername());
        mesKeys.setUpdateTime(new Date());
        return toAjax(iMesKeysService.updateById(mesKeys) ? 1 : 0);
    }

    /**
     * 删除关重件数据
     */
    @Log(title = "关重件数据" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iMesKeysService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
